﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using DoubanApi.Models;
using System.IO;
using DoubanApi.Helper;
using System.Collections.ObjectModel;

namespace DoubanApi
{
    public class DoubanApiClient
    {
        public string ApiKey { get; set; }

        #region Private
        private WebClient _WebClient;
        private DoubanDataResolve _DoubanDataResolve;
        #endregion

        #region Public Function
        public DoubanApiClient()
        {
            _WebClient = new WebClient();
            _DoubanDataResolve = new DoubanDataResolve();
        }

        public MovieModel GetMovie(int subjectID)
        {
            return GetMovie(string.Format(StringConst.MovieApiUrl, subjectID));
        }

        public MovieModel GetMovie(string subjectUrl)
        {
            string xmldata = OpenApi(subjectUrl);

            MovieModel model = new DoubanDataResolve().MovieResolve(xmldata);
            FindMovieInfo(model);
            return model;
        }

        public MovieModel SearchMovie(string pMovieName)
        {
            string aSearchUrl = string.Format(StringConst.SearchMovieUrl, pMovieName);
            string xmldata = OpenApi(aSearchUrl);
            List<string> aMovieApiAddress = new DoubanDataResolve().SearchResultResolve(xmldata);

            if (aMovieApiAddress.Count > 0)
            {
                return GetMovie(aMovieApiAddress[0]);
            }
            else
            {
                return null;
            }
        }

        public List<int> FindUserMovieFavorites(string userid)
        {

            string xmldata = OpenApi(string.Format(StringConst.UserMovieFavoritesUrl, userid));

            ObservableCollection<MovieModel> _list = new ObservableCollection<MovieModel>();

            List<int> aMovieIdList = new List<int>();
            foreach (string apiurl in new DoubanDataResolve().UserMovieFavoritesResolve(xmldata))
            {

                aMovieIdList.Add(ApiUrlConvertId(apiurl));
            }

            return aMovieIdList;
        }
        #endregion

        #region Private Function

        private string OpenApi(string url)
        {
            if (!string.IsNullOrEmpty(ApiKey))
            {
                if (url.IndexOf('?') >= 0)
                {
                    url += "&apikey=" + ApiKey;
                }
                else
                {
                    url += "?apikey=" + ApiKey;
                }
            }
            Stream stream = _WebClient.OpenRead(url);
            StreamReader streamReader = new StreamReader(stream);

            string xmldata = streamReader.ReadToEnd();
            return xmldata;
        }

        private void FindMovieInfo(MovieModel movie)
        {
            if (!string.IsNullOrEmpty(movie.DoubanLink))
            {
                string html = OpenApi(movie.DoubanLink);
                //_DoubanDataResolve.MoviePreviewResolve(html, movie);
                _DoubanDataResolve.MoviePreviewImageResolve(html, movie);
            }
            FindMoviePreviewYouKu(movie);
        }

        public void FindMoviePreviewYouKu(MovieModel movie)
        {
            foreach (string name in movie.Aka)
            {
                Stream stream = _WebClient.OpenRead(string.Format(StringConst.PreviewYoukuSearch, movie.Title));
                StreamReader streamReader = new StreamReader(stream);
                string xmldata = streamReader.ReadToEnd();
                _DoubanDataResolve.MoviePreviewYoukuResolve(xmldata, movie);
                if (!string.IsNullOrEmpty(movie.Preview.Id))
                {
                    break;
                }
            }
        }

        private int ApiUrlConvertId(string apiurl)
        {
            return int.Parse(apiurl.Substring(apiurl.LastIndexOf('/') + 1));
        }

        #endregion
    }
}
